package com.nep.entity.supervisor;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 环境监测报告实体类
 * 用于封装空气质量监测报告的相关信息
 */
public class Report {
    private String reportId;      // 报告唯一标识
    private String userId;        // 提交用户ID
    private String userName;      // 提交用户姓名
    private String aqiLevel;      // 空气质量指数级别
    private String description;   // 报告描述信息
    private LocalDateTime createTime; // 报告创建时间
    private String gridAddr;      // 监测网格地址

    // Getters and Setters
    public String getReportId() {
        return reportId;
    }

    public void setReportId(String reportId) {
        this.reportId = reportId;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getAqiLevel() {
        return aqiLevel;
    }

    public void setAqiLevel(String aqiLevel) {
        this.aqiLevel = aqiLevel;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public String getGridAddr() {
        return gridAddr;
    }

    public void setGridAddr(String gridAddr) {
        this.gridAddr = gridAddr;
    }

    /**
     * 序列化为CSV格式字符串
     * 使用逗号分隔各字段
     * 日期时间使用ISO_LOCAL_DATE_TIME格式
     * @return CSV格式字符串
     */
    public String serialize() {
        return String.join(",",
                reportId,
                userId,
                userName,
                aqiLevel,
                description,
                createTime.toString(),
                gridAddr);
    }

    /**
     * 从CSV格式字符串反序列化
     * @param line CSV格式字符串
     * @return 解析后的Report对象，如果解析失败返回null
     */
    public static Report deserialize(String line) {
        String[] parts = line.split(",");
        if (parts.length < 7) {
            // 处理行数不足的情况，记录错误日志
            System.err.println("Invalid line in reports.txt: " + line);
            return null;
        }
        Report report = new Report();
        report.setReportId(parts[0]);
        report.setUserId(parts[1]);
        report.setUserName(parts[2]);
        report.setAqiLevel(parts[3]);
        report.setDescription(parts[4]);
        // 使用ISO_LOCAL_DATE_TIME格式解析日期时间
        report.setCreateTime(LocalDateTime.parse(parts[5], DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        report.setGridAddr(parts[6]);
        return report;
    }
}